//
//  HQMapMarkerOptionsModel.m
//  o_amap
//
//  Created by Q神 on 2021/4/14.
//

#import "HQMapMarkerOptionsModel.h"
#import "MJExtension.h"
#import "HQMapTools.h"

@implementation HQMapMarkerOptionsModel

+ (NSDictionary *)mj_objectClassInArray {
    return @{@"markerOptionsList": @"MarkerOptionsModel"};
}

+ (HQMapMarkerOptionsModel *)createMarkerOptionsModelWithParams:(NSDictionary *)params {
    HQMapMarkerOptionsModel *markerOptionsModel = [[HQMapMarkerOptionsModel alloc] init];
    
    markerOptionsModel.moveToCenter = params[@"moveToCenter"];
    NSArray *arrays = [[HQMapTools shareInstance] parseJSONStringToNSArray:params[@"markerOptionsList"]];
    NSMutableArray<MarkerOptionsModel *> *lists = [NSMutableArray array];
    for (NSDictionary *dic in arrays) {
        MarkerOptionsModel *model = [MarkerOptionsModel createMarkerOptionsModelWithParams:dic];
        [lists addObject:model];
    }
    markerOptionsModel.markerOptionsList = lists;
    return markerOptionsModel;
}

- (void)addMarker:(MAMapView *)mapView {
    MarkerOptionsModel *optionsModel = self.markerOptionsList.lastObject;
    NSLog(@"latitude==longitude==%@,%@",optionsModel.position.latitude, optionsModel.position.longitude);
    double latitude = [optionsModel.position.latitude doubleValue];
    double longitude = [optionsModel.position.longitude doubleValue];
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(latitude, longitude);
    MAPointAnnotation *anno = [[MAPointAnnotation alloc] init];
    anno.coordinate = coords;
    anno.title = [NSString stringWithFormat:@"%d", 0];
    [mapView addAnnotation:anno];
    [mapView setCenterCoordinate:coords animated:YES];
}

- (void)addMarkers:(MAMapView *)mapView {
    NSMutableArray *annos = [NSMutableArray array];
    [self.markerOptionsList enumerateObjectsUsingBlock:^(MarkerOptionsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        double latitude = [obj.position.latitude doubleValue];
        double longitude = [obj.position.longitude doubleValue];
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(latitude, longitude);
        
        MAPointAnnotation *anno = [[MAPointAnnotation alloc] init];
        anno.coordinate = coords;
        /// 自定义气泡，打开如下代码，注释下面的 if 方法
//        anno.title = [NSString stringWithFormat:@"%d", i];
        if (obj.infoWindowEnable) {
            anno.title = [NSString stringWithFormat:@"预估运费%@", obj.title];
            anno.subtitle = [NSString stringWithFormat:@"全程%@公里，约行驶%@小时", obj.snippet, obj.extData.dic_time];
        }
        [annos addObject:anno];
    }];
    [mapView addAnnotations:annos];
}

@end


@implementation MarkerOptionsModel

+ (MarkerOptionsModel *)createMarkerOptionsModelWithParams:(NSDictionary *)params {
    MarkerOptionsModel *model = [[MarkerOptionsModel alloc] init];
    
    model.icon = params[@"icon"];
    model.widget = params[@"widget"];
    model.anchorU = [params[@"anchorU"] intValue];
    model.anchorV = [params[@"anchorV"] intValue];
    model.draggable = params[@"draggable"];
    model.infoWindowEnable = [params[@"infoWindowEnable"] boolValue];
    model.position = [LatLngModel mj_objectWithKeyValues:[[HQMapTools shareInstance] hq_jsonStringCompactFormatForDictionary:params[@"position"]]];
    model.infoWindowOffsetX = [params[@"infoWindowOffsetX"] intValue];
    model.infoWindowOffsetY = [params[@"infoWindowOffsetY"] intValue];
    model.snippet = params[@"snippet"];
    model.title = params[@"title"];
    model.lockedToScreen = params[@"lockedToScreen"];
    model.lockedScreenPoint = params[@"lockedScreenPoint"];
    model.customCalloutView = params[@"customCalloutView"];
    model.enabled = params[@"enabled"];
    model.highlighted = params[@"highlighted"];
    model.selected = params[@"selected"];
    model.leftCalloutAccessoryView = params[@"leftCalloutAccessoryView"];
    model.rightCalloutAccessoryView = params[@"rightCalloutAccessoryView"];
    model.customCalloutView = params[@"customCalloutView"];
    model.customCalloutView = params[@"customCalloutView"];
    model.extData = [HQMapMakerExtData mj_objectWithKeyValues:[[HQMapTools shareInstance] hq_jsonStringCompactFormatForDictionary:params[@"extData"]]];
    
    return model;
}

@end

